#include "include/dataBase.h"

#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include <QDebug>

DataBase::DataBase()
{

}

bool DataBase::CreateDataBase(const QString &dbName)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(dbName);

    if (!db.open())
    {
        QMessageBox::critical(0, QObject::tr("Can't open database"),
                  QObject::tr("Unable to establish a database connection.\n"
                     "This example needs SQLite support. Please read "
                     "the Qt SQL driver documentation for information how "
                     "to build it.\n\n"
                     "Click Cancel to exit."), QMessageBox::Cancel);
        return false;
    }

    QString createItmInfo = "create table itemInfo ("
                            "id integer primary key autoincrement,"
                            "area varchar(4),"
                            "ridgepole varchar(4),"
                            "layer varchar(4),"
                            "room varchar(4),"
                            "devName varchar(20)"
                            ")";

    QString createUsrInfo = "create table usrInfo ("
                            "usrName varchar(20),"
                            "passWord varchar(20)"
                            ")";
    QSqlQuery query;
    query.exec(createItmInfo);
    query.exec(createUsrInfo);

    return true;
}

void DataBase::CreateInitData()
{
    QSqlDatabase::database().transaction();
    QSqlQuery query;

    for(int i=0; i<3600; i++)
    {
        switch (i%50)
        {

            case 0:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '01层', '101房', '排烟阀')").arg(i));
                break;
            }
            case 1:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '01层', '101房', '消防水泵阀')").arg(i));
                break;
            }
            case 2:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '01层', '102房', '排烟阀')").arg(i));
                break;
            }
            case 3:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '01层', '102房', '消防水泵阀')").arg(i));
                break;
            }
            case 4:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '02层', '201房', '排烟阀')").arg(i));
                break;
            }
            case 5:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '02层', '201房', '消防水泵阀')").arg(i));
                break;
            }
            case 6:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '02层', '202房', '排烟阀')").arg(i));
                break;
            }
            case 7:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '02层', '202房', '消防水泵阀')").arg(i));
                break;
            }
            case 8:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '03层', '301房', '排烟阀')").arg(i));
                break;
            }
            case 9:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '03层', '301房', '消防水泵阀')").arg(i));
                break;
            }
            case 10:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '03层', '302房', '排烟阀')").arg(i));
                break;
            }
            case 11:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '01栋', '03层', '302房', '消防水泵阀')").arg(i));
                break;
            }
            case 12:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '01层', '101房', '排烟阀')").arg(i));
                break;
            }
            case 13:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '01层', '101房', '消防水泵阀')").arg(i));
                break;
            }
            case 14:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '01层', '102房', '排烟阀')").arg(i));
                break;
            }
            case 15:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '01层', '102房', '消防水泵阀')").arg(i));
                break;
            }
            case 16:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '02层', '201房', '排烟阀')").arg(i));
                break;
            }
            case 17:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '02层', '201房', '消防水泵阀')").arg(i));
                break;
            }
            case 18:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '02层', '202房', '排烟阀')").arg(i));
                break;
            }
            case 19:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '02层', '202房', '消防水泵阀')").arg(i));
                break;
            }
            case 20:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '03层', '301房', '排烟阀')").arg(i));
                break;
            }
            case 21:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '03层', '301房', '消防水泵阀')").arg(i));
                break;
            }
            case 22:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '03层', '302房', '排烟阀')").arg(i));
                break;
            }
            case 23:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '03层', '302房', '消防水泵阀')").arg(i));
                break;
            }
            case 24:
            {
                query.exec(QString("insert into itemInfo values(%1, 'A区', '02栋', '03层', '101房', '排烟阀')").arg(i));
                break;
            }
            case 25:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '01层', '101房', '排烟阀')").arg(i));
                break;
            }
            case 26:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '01层', '101房', '消防水泵阀')").arg(i));
                break;
            }
            case 27:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '01层', '102房', '排烟阀')").arg(i));
                break;
            }
            case 28:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '01层', '102房', '消防水泵阀')").arg(i));
                break;
            }
            case 29:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '02层', '201房', '排烟阀')").arg(i));
                break;
            }
            case 30:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '02层', '201房', '消防水泵阀')").arg(i));
                break;
            }
            case 31:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '02层', '202房', '排烟阀')").arg(i));
                break;
            }
            case 32:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '02层', '202房', '消防水泵阀')").arg(i));
                break;
            }
            case 33:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '03层', '301房', '排烟阀')").arg(i));
                break;
            }
            case 34:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '03层', '301房', '消防水泵阀')").arg(i));
                break;
            }
            case 35:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '03层', '302房', '排烟阀')").arg(i));
                break;
            }
            case 36:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '01栋', '03层', '302房', '消防水泵阀')").arg(i));
                break;
            }
            case 37:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '01层', '101房', '排烟阀')").arg(i));
                break;
            }
            case 38:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '01层', '101房', '消防水泵阀')").arg(i));
                break;
            }
            case 39:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '01层', '102房', '排烟阀')").arg(i));
                break;
            }
            case 40:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '01层', '102房', '消防水泵阀')").arg(i));
                break;
            }
            case 41:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '02层', '201房', '排烟阀')").arg(i));
                break;
            }
            case 42:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '02层', '201房', '消防水泵阀')").arg(i));
                break;
            }
            case 43:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '02层', '202房', '排烟阀')").arg(i));
                break;
            }
            case 44:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '02层', '202房', '消防水泵阀')").arg(i));
                break;
            }
            case 45:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '03层', '301房', '排烟阀')").arg(i));
                break;
            }
            case 46:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '03层', '301房', '消防水泵阀')").arg(i));
                break;
            }
            case 47:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '03层', '302房', '排烟阀')").arg(i));
                break;
            }
            case 48:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '03层', '302房', '消防水泵阀')").arg(i));
                break;
            }
            case 49:
            {
                query.exec(QString("insert into itemInfo values(%1, 'B区', '02栋', '03层', '101房', '排烟阀')").arg(i));
                break;
            }
            default:
            {
                break;
            }
        }
    }

    query.exec(QString("insert into usrInfo(usrName, passWord) values('root', 'root')"));
    query.exec(QString("insert into usrInfo(usrName, passWord) values('admin', 'admin' )"));

    QSqlDatabase::database().commit();
}
